package com.briup.service.Impl;

import com.briup.bean.Department;
import com.briup.bean.DepartmentExample;
import com.briup.dao.DepartmentMapper;
import com.briup.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author wyq
 * @create 2021-09-24 10:58
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    public int deleteById(int id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

    /**
     * Description: TODO  根据部门名查询部门，如果部门名为null，则查询全部
     *
     * @author: wyq
     * @date: 2021/9/24 11:16
     * @Param name: 部门名
     * @return: java.util.List<com.briup.bean.Department>
     */

    public List<Department> findByName(String name) {
        DepartmentExample example;
        if (!StringUtils.isEmpty(name)) {
            example = new DepartmentExample();
            example.createCriteria().andDeptNameLike("%" + name + "%");
            return departmentMapper.selectByExample(example);
        }
        return departmentMapper.selectByExample(null);
    }

    public Department findById(int id) {
        return departmentMapper.selectByPrimaryKey(id);
    }


    public int saveOrUpdate(Department department) {
        if (StringUtils.isEmpty(department.getDeptId())) {
            return departmentMapper.insertSelective(department);
        }
        return departmentMapper.updateByPrimaryKeySelective(department);
    }
}
